<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="query">
                <el-form-item>
                    <el-input v-model="query.keywords" placeholder="宠物名"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-select clearable v-model="query.state" placeholder="宠物状态">
                        <el-option label="待处理" value="0"></el-option>
                        <el-option label="已处理" value="1"></el-option>
                        <el-option label="重新提交" value="-1"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="search">查询</el-button>
                </el-form-item>
            </el-form>
        </el-col>
        <!--审核界面-->
        <el-dialog title="审核" :visible.sync="formVisible" :close-on-click-modal="false">
            <el-form :model="dataForm" label-width="80px">
                <el-form-item label="宠物品种" >
                    <el-input v-model="dataForm.title" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="宠物名字" >
                    <el-input v-model="dataForm.name" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="宠物价格" >
                    <el-input v-model="dataForm.price" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="宠物年龄" >
                    <el-input v-model="dataForm.age" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="宠物图片" >
                    <el-input v-model="dataForm.resources" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="驳回原因">
                    <el-input type="textarea" v-model="dataForm.reason"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="formVisible = false">取消</el-button>
                <el-button type="success" @click.native="pass" :loading="addLoading">通过</el-button>
                <el-button type="danger" @click.native="reject" :loading="addLoading">驳回</el-button>
            </div>
        </el-dialog>
        <!--列表 -->
        <el-table :data="listData" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="title" label="宠物品种" width="80" >
            </el-table-column>
            <el-table-column prop="name" label="宠物名字" width="80" >
            </el-table-column>
            <el-table-column prop="price" label="宠物价格" width="80" >
            </el-table-column>
            <el-table-column prop="age" label="宠物年龄" width="80" >
            </el-table-column>
            <el-table-column prop="gender" label="宠物性别" width="80" >
                <template scope="scope">
                    <span v-if="scope.row.state==0">母</span>
                    <span v-if="scope.row.state==1">公</span>
                </template>
            </el-table-column>
            <el-table-column prop="coat_color" label="宠物毛色" width="80" >
            </el-table-column>
            <el-table-column prop="resources" label="宠物图片" width="100" >
            </el-table-column>
            <el-table-column prop="pet_type" label="宠物分类" min-width="80" >
                <template scope="scope">
                    <span v-if="scope.row.state==0">狗</span>
                    <span v-if="scope.row.state==1">猫</span>
                </template>
            </el-table-column>
            <el-table-column prop="address" label="用户地址" width="120" >
            </el-table-column>
            <el-table-column prop="state" label="状态" min-width="80" >
                <template scope="scope">
                    <span v-if="scope.row.state==0" style="color: #8c939d">待处理</span>
                    <span v-if="scope.row.state==1" style="color: #13ce66">已处理</span>
                    <span v-if="scope.row.state==-1" style="color: #ff4d51">重新提交</span>
                </template>
            </el-table-column>
            <el-table-column prop="user.username" label="用户姓名" min-width="120"  >
            </el-table-column>
            <el-table-column label="操作" width="250">
                <template scope="scope">
                    <el-button type="warning" size="small" :disabled="scope.row.state==1" @click="check(scope.$index, scope.row)">审核</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="query.pageSize"
                           :total="total" style="float:right;">
            </el-pagination>
        </el-col>
    </section>
</template>
<script>
    export default {
        data(){
            return {
                listLoading:false, // 开启加载效果
                listData:[],
                sels:[],
                total:0, // 总条数
                query:{
                    pageSize:5,
                    currentPage:1,
                    keywords:'',
                },
                addFormVisible: false, // 默认不显示弹出框
                addLoading: false,
                formVisible: false,
                addFormRules: {
                    // 验证规则
                    // required: true：  要求：必填
                    // message：  出错时提示什么消息
                    // trigger:'blur'  触发规则：
                    name: [
                        { required: true, message: '请输入部门名称', trigger: 'blur' }
                    ],
                    sn: [
                        { required: true, message: '请输入部门编号', trigger: 'blur' }
                    ]

                },
                dataForm:{
                    reason: ''
                },
                employees: []
            }
        },
        methods:{
            // 审核
            check(index, row){
                this.dataForm = Object.assign({},row);
                this.formVisible = true;
            },
            // 驳回方法
            reject(){
                if(!this.dataForm.reason){
                    this.$message.error('请输入驳回信息');
                    return;
                }
                // 驳回的信息
                let param = {
                    shop_id: this.dataForm.id,
                    note: this.dataForm.reason,
                }
                // 发送请求
                this.$http.post("/searchMasterMsg/reject",param).then(res=>{
                    let {success,message} = res.data;
                    if(success){
                        this.$message({
                            message: message,
                            type: 'success'
                        })
                    } else {
                        this.$message.error('网络异常');
                    }
                    // 关闭弹出框
                    this.formVisible=false;
                    // 重新加载列表
                    this.loadListData();
                })
            },
            pass(){
                //
                let param = {
                    shop_id: this.dataForm.id,
                    note: ''
                };
                this.$http.post("/searchMasterMsg/pass",param).then(res=>{
                    let {success,message} = res.data;
                    if(success){
                        this.$message({
                            message: message,
                            type: 'success'
                        })
                    } else {
                        this.$message.error('网络异常');
                    }
                    // 关闭弹出框
                    this.formVisible=false;
                    // 重新加载列表
                    this.loadListData();
                })
            },
            // 批量勾选
            selsChange(val){
                this.sels = val;
            },
            loadListData(){
                // 发送请求
                this.$http.post("/searchMasterMsg",this.query).then(res=>{
                    console.debug(res);
                    this.listData = res.data.data;
                    this.total = res.data.total;
                })
            },
            loadEmpData(){
              // 发送请求
              this.$http.get("/emp").then(res=>{
                  // console.debug(res);
                  this.employees = res.data;
              })
            },
            // 批量删除
            batchRemove(){
                var ids = this.sels.map(item => item.id);
                // 弹出确认框
                this.$confirm('确认删除？', '提示', {
                        confirmButtonText: '删除',
                        cancelButtonText: '取消',
                        type: 'warning'
                }).then(()=>{
                    // 发送请求
                    this.$http.patch("/dept/",ids).then(res=>{
                        // 返回信息
                        let {success,message}=res.data;
                        console.debug(success,message);
                        if (success){
                            this.$message({
                                message: message,
                                type: 'success'
                            });
                        } else {
                            this.$message.error('网络异常');
                        }
                        // 重新加载列表
                        this.loadListData();
                    })
                }).catch(()=>{  // 选择取消回调方法
                    this.$message('已取消');
                })
            },
            // 分页方法
            handleCurrentChange(val){
                this.query.currentPage=val;
                this.loadListData();
            },
            // 高级查询
            search(){
                // 设置初始页面为1
                this.query.currentPage=1;
                // 页面加载
                this.loadListData();
            },
            // 删除
            handleDel(index, row) {
                // 弹出确认框
                this.$confirm('确认删除？', '提示', {
                    confirmButtonText: '删除',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(()=>{ // 选择确认的回调方法
                    // 发送请求
                    this.$http.delete("/searchMasterMsg/"+row.id).then(res=>{
                        // 返回信息
                        let {success,message}=res.data;
                        if (success){
                            this.$message({
                                message: message,
                                type: 'success'
                            });
                        } else {
                            this.$message.error('网络异常');
                        }
                        console.debug(success,message);
                        // 设置初始页面为1
                        this.query.currentPage=1;
                        // 重新加载列表
                        this.loadListData();
                    })
                }).catch(()=>{  // 选择取消回调方法
                    this.$message('已取消');
                })
            },
            // 提交
            addSubmit(){
                listLoading: true // 开启加载效果
                // 发送请求
                this.$http.put("searchMasterMsg",this.addForm).then(res=>{
                    // console.debug(res);
                    // 返回信息
                    let {success,message}=res.data;
                    if (success){
                        this.$message({
                            message: message,
                            type: 'success'
                        });
                    } else {
                        this.$message.error('网络异常');
                    }
                    listLoading: false // 关闭加载效果
                    // 关闭弹出框
                    this.addFormVisible = false;
                    // 重新加载页面
                    this.loadListData();
                }).catch(()=>{  // 选择取消回调方法
                    this.$message('已取消');
                })
            }
        },
        mounted(){
            // 页面加载
            this.loadListData();
            // 加载负责人下拉框数据
            this.loadEmpData();
        }
    }
</script>

<style>

</style>